package cn.web.base.activemq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import javax.jms.*;
import java.io.Serializable;
import java.util.Map;

public class MessageSender {

	private JmsTemplate jmsTemplate;

	public MessageSender(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;

	}

	public void sendTextMessage(final String txtMessage) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(txtMessage);
			}
		});
	}

	public void sendTextMessage(final String queueName, final String txtMessage) {
		jmsTemplate.send(queueName, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(txtMessage);
			}
		});
	}

	public void sendTextMessage(final Destination destination, final String txtMessage) {
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(txtMessage);
			}
		});
	}

	public void sendObjectMessage(final Object objectMessage) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createObjectMessage((Serializable) objectMessage);
			}
		});
	}

	public void sendObjectMessage(final String queueName, final Object objectMessage) {
		jmsTemplate.send(queueName, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createObjectMessage((Serializable) objectMessage);
			}
		});
	}

	public void sendObjectMessage(final Destination destination, final Object objectMessage) {
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createObjectMessage((Serializable) objectMessage);
			}
		});
	}

	public void sendMapMessage(final Map<String, Object> mapMessage) {
		jmsTemplate.send(new MessageCreator() {
			@SuppressWarnings("unchecked")
			public Message createMessage(Session session) throws JMSException {
				MapMessage _mapMessage = session.createMapMessage();
				for (Map.Entry<String, Object> entry : ((Map<String, Object>) mapMessage).entrySet()) {
					_mapMessage.setObject(entry.getKey(), entry.getValue());
				}
				return _mapMessage;
			}
		});
	}

	public void sendMapMessage(final String queueName, final Map<String, Object> mapMessage) {
		jmsTemplate.send(queueName, new MessageCreator() {
			@SuppressWarnings("unchecked")
			public Message createMessage(Session session) throws JMSException {
				MapMessage _mapMessage = session.createMapMessage();
				for (Map.Entry<String, Object> entry : ((Map<String, Object>) mapMessage).entrySet()) {
					_mapMessage.setObject(entry.getKey(), entry.getValue());
				}
				return _mapMessage;
			}
		});
	}

	public void sendMapMessage(final Destination destination, final Map<String, Object> mapMessage) {
		jmsTemplate.send(destination, new MessageCreator() {
			@SuppressWarnings("unchecked")
			public Message createMessage(Session session) throws JMSException {
				MapMessage _mapMessage = session.createMapMessage();
				for (Map.Entry<String, Object> entry : ((Map<String, Object>) mapMessage).entrySet()) {
					_mapMessage.setObject(entry.getKey(), entry.getValue());
				}
				return _mapMessage;
			}
		});
	}

	public void sendByteMessage(final byte[] message) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				BytesMessage bytesMessage = session.createBytesMessage();
				bytesMessage.writeBytes(message);
				return bytesMessage;
			}
		});
	}


	public void sendByteMessage(final String queueName, final byte[] message) {
		jmsTemplate.send(queueName, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				BytesMessage bytesMessage = session.createBytesMessage();
				bytesMessage.writeBytes(message);
				return bytesMessage;
			}
		});
	}

	public void sendByteMessage(final Destination destination, final byte[] message) {
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				BytesMessage bytesMessage = session.createBytesMessage();
				bytesMessage.writeBytes(message);
				return bytesMessage;
			}
		});
	}

	public void sendStreamMessage(final String queueName, final Object message) {
		jmsTemplate.send(queueName, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				StreamMessage streamMessage = session.createStreamMessage();
				streamMessage.writeObject(message);
				return streamMessage;
			}
		});
	}

	public void sendStreamMessage(final Object message) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				StreamMessage streamMessage = session.createStreamMessage();
				streamMessage.writeObject(message);
				return streamMessage;
			}
		});
	}

	public void sendStreamMessage(final Destination destination, final Object message) {
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				StreamMessage streamMessage = session.createStreamMessage();
				streamMessage.writeObject(message);
				return streamMessage;
			}
		});
	}


}
